package com.itheima.test01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/add")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String score = request.getParameter("score");

        String path = getServletContext().getRealPath("student.txt");
        FileWriter fos = new FileWriter(path,true);
        fos.write(name+","+age+","+score);
        fos.write(System.lineSeparator());
        fos.close();

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("添加成功,2秒后跳转到首页");
        response.setHeader("Refresh","2;URL=/test/student.html");
    }
}
